Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - [VB.NET + XNA]Rettangolo che delimita l'area occupata dall'oggetto
Forum - C# / VB.NET - [VB.NET + XNA]Rettangolo che delimita l'area occupata dall'oggetto

Avatar
Nullable (Normal User)
Expert


Messaggi: 217
Iscritto: 12/07/2011

Segnala al moderatore
Postato alle 21:16
Domenica, 22/07/2012
Ciao ragazzi, giusto l'altro ieri mi è venuta voglia di provare il framework XNA quindi sono partito dalla guida di Totem ( http://totemslair.org/guide/xna.php ), dopo aver installato XNA Game Studio 4.0, copiando il sorgente dell'esempio e provarlo sul mio computer. Tutto fila liscio eccetto la pallina...nella funzione CollisionCheck di cui vi riporto il codice leggermente modifcato :

Codice sorgente - presumibilmente VB.NET

  1. Private Sub CollisionCheck()
  2.         Dim BatRect As Rectangle = New Rectangle(Bat.Position.X - (Bat.Sprite.Width / 2), Bat.Position.Y - (Bat.Sprite.Width / 2), Bat.Sprite.Width, Bat.Sprite.Height)
  3.         Dim BallRect As Rectangle = New Rectangle(Ball.Position.X - (Ball.Sprite.Width / 2), Ball.Position.Y - (Ball.Sprite.Height / 2), Ball.Sprite.Width, Ball.Sprite.Height)
  4.  
  5.         If BallRect.Intersects(BatRect) Then
  6.             Ball.Velocity = New Vector2(Ball.Velocity.X, -Ball.Velocity.Y)
  7.         End If
  8.  
  9.         If ((Ball.Position.X > 5) Or (Ball.Position.X > Me.GraphicsDevice.Viewport.Width - 5)) And (Ball.Position.Y < Me.GraphicsDevice.Viewport.Height - 5) Then
  10.             Ball.Velocity += New Vector2(Ball.Position.X, -Ball.Position.Y)
  11.         End If
  12.     End Sub



mi restituisce un'eccezione nella seconda riga ovvero quando tenta di creare il rettangolo che delimita l'area occupata dall'oggetto, l'eccezione è di tipo OverflowException ed il messaggio è il seguente :

Overflow di un'operazione aritmetica, e mi accorgo che il numero dato dall'esecuzione di queste due istruzioni : Ball.Position.X - (Ball.Sprite.Width / 2) e Ball.Position.Y - (Ball.Sprite.Height / 2) è uguale a 0...non capisco il perché, è come se non venisse disegnata e posizionata la pallina, però se provo a fare il debug step by step ( con F11 ) l'errore non si verifica...sapete dirmi perché ?

NB: La classe GameObject è la stessa di quella di Totem quindi non la posto, piuttosto vi posto l'intero codice, nonché il resto del programma perché magari sbaglio l'ordine di istruzioni ( anche se non penso...me ne sarei accorto ) :

Codice sorgente - presumibilmente VB.NET

  1. Public Class TryGame
  2.  
  3.     Inherits Microsoft.Xna.Framework.Game
  4.  
  5.     Private WithEvents graphics As GraphicsDeviceManager
  6.     Private WithEvents spriteBatch As SpriteBatch
  7.     Private BatSpeed As Single = 3
  8.     Private BallSpeed As Single = 3
  9.     Private Bat As GameObject
  10.     Private Ball As GameObject
  11.  
  12. #Region "My methods"
  13.  
  14.     Private Function GetTexture(ByVal Name As String) As Texture2D
  15.         Dim ImageStream As New System.IO.FileStream(My.Application.Info.DirectoryPath & "\" & Name, IO.FileMode.Open)
  16.         Return Texture2D.FromStream(Me.graphics.GraphicsDevice, ImageStream)
  17.     End Function
  18.  
  19.     Private Sub KeyboardCheck()
  20.         Dim KeyState As KeyboardState = Keyboard.GetState()
  21.  
  22.         If KeyState.IsKeyDown(Keys.Right) Then
  23.             Bat.Position += New Vector2(BatSpeed, 0)
  24.         End If
  25.  
  26.         If KeyState.IsKeyDown(Keys.Left) Then
  27.             Bat.Position += New Vector2(-BatSpeed, 0)
  28.         End If
  29.  
  30.         If KeyState.IsKeyDown(Keys.Escape) Then
  31.             Me.Exit()
  32.         End If
  33.     End Sub
  34.  
  35.     Private Sub CollisionCheck()
  36.         Dim BatRect As Rectangle = New Rectangle(Bat.Position.X - (Bat.Sprite.Width / 2), Bat.Position.Y - (Bat.Sprite.Width / 2), Bat.Sprite.Width, Bat.Sprite.Height)
  37.         Dim BallRect As Rectangle = New Rectangle(Ball.Position.X - (Ball.Sprite.Width / 2), Ball.Position.Y - (Ball.Sprite.Height / 2), Ball.Sprite.Width, Ball.Sprite.Height)
  38.  
  39.         If BallRect.Intersects(BatRect) Then
  40.             Ball.Velocity = New Vector2(Ball.Velocity.X, -Ball.Velocity.Y)
  41.         End If
  42.  
  43.         If ((Ball.Position.X > 5) Or (Ball.Position.X > Me.GraphicsDevice.Viewport.Width - 5)) And (Ball.Position.Y < Me.GraphicsDevice.Viewport.Height - 5) Then
  44.             Ball.Velocity += New Vector2(Ball.Position.X, -Ball.Position.Y)
  45.         End If
  46.     End Sub
  47.  
  48. #End Region
  49.  
  50.     Public Sub New()
  51.         graphics = New GraphicsDeviceManager(Me)
  52.         Content.RootDirectory = "Content"
  53.     End Sub
  54.  
  55.     Protected Overrides Sub Initialize()
  56.         MyBase.Initialize()
  57.         spriteBatch = New SpriteBatch(GraphicsDevice)
  58.     End Sub
  59.  
  60.     Protected Overrides Sub LoadContent()
  61.         MyBase.LoadContent()
  62.  
  63.         Bat = New GameObject(GetTexture("baton.png"))
  64.         Bat.Position = New Vector2(Me.GraphicsDevice.Viewport.Width / 2, Me.GraphicsDevice.Viewport.Height - 40)
  65.  
  66.         Ball = New GameObject(GetTexture("ball.png"))
  67.         Ball.Position = New Vector2(Me.GraphicsDevice.Viewport.Width / 2, Me.GraphicsDevice.Viewport.Height / 2)
  68.         Ball.Velocity = New Vector2(0, BallSpeed)
  69.     End Sub
  70.  
  71.     Protected Overrides Sub UnloadContent()
  72.         MyBase.UnloadContent()
  73.     End Sub
  74.  
  75.     Protected Overrides Sub Update(ByVal gameTime As GameTime)
  76.         KeyboardCheck()
  77.         CollisionCheck()
  78.         Ball.Position += Ball.Velocity
  79.         If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
  80.             Me.Exit()
  81.         End If
  82.         MyBase.Update(gameTime)
  83.     End Sub
  84.  
  85.     Protected Overrides Sub Draw(ByVal gameTime As GameTime)
  86.         GraphicsDevice.Clear(Color.Blue)
  87.  
  88.         spriteBatch.Begin()
  89.  
  90.         Bat.Draw(spriteBatch)
  91.         Ball.Draw(spriteBatch)
  92.  
  93.         spriteBatch.End()
  94.         MyBase.Draw(gameTime)
  95.     End Sub
  96.  
  97. End Class


PM Quote